﻿
var masterData;
var selectedRoomtypes = [];
var selectedRateplans = [];
var selectedOccupancies = [];
var bulkFilter;
var selectedDaysofweek = [];
var selectedAttributes = [];

var uri = '/hotel/';

function DisplayHotelList() {
    for (var h = 0; h <= masterData.length - 1; h++) {
        if (h == 0)
            $("#HotelOptions").append("<option value='" + masterData[h].HotelId + "' selected='selected'>" + masterData[h].HotelName + "</option>");
        else
            $("#HotelOptions").append("<option value='" + masterData[h].HotelId + "'>" + masterData[h].HotelName + "</option>");
    }
}

function GenerateRatePlans(rateplans) {
    $('#DivRatePlans').empty();
    $.each(rateplans, function (j, itemrateplan) {
        var rateplanHtml = "<input type='checkbox' onchange='OnRatePlanSelect()' value=" + itemrateplan.BoardId + " checked>" + itemrateplan.Board + "</label>";
        if (j == 0) {
            $('#DivRatePlans').append("<label class='btn btn-default active'>" + rateplanHtml);
            selectedRateplans.push(itemrateplan.BoardId)
        }
        else {
            $('#DivRatePlans').append("<label class='btn btn-default'>" + rateplanHtml);
        }
    })
}

function OnRoomTypeSelect() {
    selectedRoomtypes = [];
    $("#DivRoomTypes > label > input").each(function () {
        if ($(this)[0].checked == true)
            selectedRoomtypes.push($(this)[0].defaultValue);
    })

    var allRatePlans = [];
    selectedRateplans = [];
    var roomrateplans = {};
    for (var i = 0; i <= selectedRoomtypes.length - 1; i++) {

        for (var r = 0; r <= masterData[0].Rooms.length - 1; r++) {
            if (masterData[0].Rooms[r].RoomId == selectedRoomtypes[i]) {
                allRatePlans = $.merge(allRatePlans, masterData[0].Rooms[r].RatePlans);
                break;
            }
        }

    }
    allRatePlans = jQuery.unique(allRatePlans);
    //allRatePlans = allRatePlans.unique();
    GenerateRatePlans(allRatePlans);
}

function GetUnique(inputArray) {
    var outputArray = [];

    for (var i = 0; i < inputArray.length; i++) {
        if ((jQuery.inArray(inputArray[i], outputArray)) == -1) {
            outputArray.push(inputArray[i]);
        }
    }

    return outputArray;
}

function OnRatePlanSelect() {
    selectedRateplans = [];
    $("#DivRatePlans > label > input").each(function () {
        if ($(this)[0].checked == true)
            selectedRateplans.push($(this)[0].defaultValue);
    })
}

function OnOccupancySelect() {
    selectedOccupancies = [];
    $("#DivOccupancies > label > input").each(function () {
        if ($(this)[0].checked == true)
            selectedOccupancies.push($.trim($(this)[0].parentNode.outerText));
    })

}

function OnSelectBulkFilter(obj) {
    //if (obj == 'yes') {
    //    if ($.trim($('#bulkYes')[0].parentNode.className) == 'btn btn-default') {
    //        $('#bulkYes')[0].parentNode.removeClass("btn btn-default");
    //        $('#bulkYes')[0].parentNode.addClass("btn btn-default active");
    //       $('#bulkNo')[0].parentNode.removeClass("btn btn-default active");
    //        $('#bulkNo')[0].parentNode.addClass("btn btn-default");
    //    }
    //}

    //if (obj == 'no') {
    //    if ($.trim($('#bulkNo')[0].parentNode.className) == 'btn btn-default') {
    //        $.trim($('#bulkNo')[0].parentNode.removeClass("btn btn-default"));
    //        $.trim($('#bulkNo')[0].parentNode.addClass("btn btn-default active"));
    //        $.trim($('#bulkYes')[0].parentNode.removeClass("btn btn-default active"));
    //        $.trim($('#bulkYes')[0].parentNode.addClass("btn btn-default"));
    //    }
    //}
}

function OnAttributesSelect() {
    selectedAttributes = [];
    $("#DivAttributes > label > input").each(function () {
        if ($(this)[0].checked == true)
            selectedAttributes.push($.trim($(this)[0].parentNode.outerText));
    })
}

function OnDaysOfWeekSelect() {
    selectedDaysofweek = [];
    $("#DivDaysOfWeek > label > input").each(function () {
        if ($(this)[0].checked == true)
            selectedDaysofweek.push($.trim($(this)[0].parentNode.outerText));
    })
}

function ShowPriceGridResults() {
    $('#DivPriceGrid').empty();

    OnAttributesSelect();
    OnDaysOfWeekSelect();
    OnOccupancySelect();

    CreateTable();
}

function CreateGridColumns() {
    var tableHead = "<thead><tr><th></th>";
    for (var i = 0; i <= selectedAttributes.length - 1; i++) {
        tableHead = tableHead + "<th>" + selectedAttributes[i] + "</th>";
    }

    tableHead = tableHead + "</tr></thead>";
    return tableHead;
}

function CreateGridRows() {
    var tableBody = "<tbody>";

    var start = new Date($("#fromdate").datepicker().val());
    var end = new Date($("#todate").datepicker().val());

    while (start < end) {
        tableBody = tableBody + "<tr><td>" + start.getDate() + "/" + start.getMonth() + "/" + start.getYear() + "</td>";
        for (var i = 0; i <= selectedAttributes.length - 1; i++) {
            if (selectedAttributes[i] == 'Allocation' || selectedAttributes[i] == 'Current Net Rate' || selectedAttributes[i] == 'Stop Sale')
                tableBody = tableBody + "<td>" + "<input type='text' />" + "</td>";
            else
                tableBody = tableBody + "<td>" + "" + "</td>";
        }
        var newDate = start.setDate(start.getDate() + 1);
        start = new Date(newDate);
        tableBody = tableBody + "</tr>";
    }

    tableBody = tableBody + "</tbody>";
    return tableBody;
}

function CreateTable() {
    var collapseNo = 1;
    var j = 0;
    var table = "";

    $.each(masterData[0].Rooms, function (i, itemRoom) {
        var roomnameRateplan = itemRoom.RoomName;
        $.each(itemRoom.RatePlans, function (k, itemRatePlan) {
            for (var i = 0; i <= selectedOccupancies.length - 1; i++) {
                table = "";

                var row1 = "<div class='panel panel-default'><div class='panel-heading'><h4 class='panel-title'><a data-toggle='collapse' data-parent='#accordion' href='#collapse" + j + "'>";
                var row2 = "</a></h4></div><div id='collapse" + j + "' class='panel-collapse collapse" + "showcollapse" + "'><div class='panel-body'><table class='table table-bordered table-condensed table-striped'>";
                var row3 = "</table></div></div></div>";

                if (j == 0)
                    row2 = row2.replace("showcollapse", " in");
                else
                    row2 = row2.replace("showcollapse", "");

                roomnameRateplan = itemRoom.RoomName + " - " + itemRatePlan.Board + " - " + selectedOccupancies[i];
                table = row1 + roomnameRateplan + row2 + CreateGridColumns() + CreateGridRows() + row3;
                if (j == 0) {
                    var firstTable = "<div class='panel-group' id='accordion'>";
                    table = firstTable + table + "</div>";
                }
                $('#DivPriceGrid').append(table);
                j = j + 1;
            }
        });
    })
}

function SavePriceGridData() {

    //$.ajax({
    //    url: 'hotel/hotelid/inventory',
    //    type: 'POST',
    //    dataType: "json",
    //    data: masterData,
    //    success: function (data) {

    //        var s = data;

    //    },
    //    error: function (data) {
    //        console.log(data);
    //    }
    //});
}

/*

$.ajax({
    url: 'hotel/d5614625-857f-487e-b98b-8726e8337321/?type=json',
    type: 'GET',
    dataType: "json",
    success: function (data) {
        masterData = data;

        $.each(data[0].Rooms, function (i, item) {
            if (i == 0) {
                $('#DivRoomTypes').append("<label class='btn btn-default active'><input type='checkbox' onchange='OnRoomTypeSelect(this)' value=" + item.RoomId + " checked>" + item.RoomName + "</label>");
                selectedRoomtypes.push(item.RoomId);

                // GenerateRatePlans();
                OnAttributesSelect();
                OnDaysOfWeekSelect();
                OnOccupancySelect();
            }
            else {
                $('#DivRoomTypes').append("<label class='btn btn-default'><input type='checkbox' onchange='OnRoomTypeSelect(this)' value=" + item.RoomId + " checked>" + item.RoomName + "</label>");
            }

        })


        // var g = data;
    },
    error: function (data) {
        console.log(data);
    }
});

*/